"""
Case Type   : HugePage
Case Name   : enable_huge_pages参数设置为on，系统大页数量、系统大页权限不同场景下，启动数据库验证
Create At   : 2024/10/21
Owner       : liu-tong-8848
Description :
              1、配置shared_buffers=128MB
              2、stop数据库集群
              3、系统配置大页数量为0，数据库用户属组无使用大页权限配置
              4、打开大页开关，设置enable_huge_pages参数为1，重启数据库
              5、系统配置大页数量大于0，数据库用户属组无使用大页权限配置
              6、重启数据库
              7、系统配置大页数量等于0，数据库用户属组有使用大页权限配置
              8、重启数据库
              9、系统配置大页数量大于步骤8提示的required pages，数据库用户属组有使用大页权限配置
              10、重启数据库
              11、清理环境
Expect      :
              1、配置shared_buffers=128MB，成功
              2、stop数据库集群，成功
              3、系统配置大页数量为0，数据库用户属组无使用大页权限配置，成功
              4、打开大页开关，设置enable_huge_pages参数为1，重启数据库失败，合理报错
              5、系统配置大页数量大于0，数据库用户属组无使用大页权限配置，成功
              6、重启数据库失败，合理报错
              7、系统配置大页数量等于0，数据库用户属组有使用大页权限配置，成功
              8、重启数据库失败，合理报错
              9、系统配置大页数量大于步骤8提示的required pages，数据库用户属组有使用大页权限配置，成功
              10、重启数据库，成功
              11、清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class GucTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        self.com = Common('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.db_user_node = Node(node='PrimaryDbUser')
        self.db_root_node = Node(node='PrimaryRoot')
        self.log.info("查询shared_buffers默认值")
        self.shared_buffers_original_result = self.com.show_param("shared_buffers")

    def test_startdb(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:配置shared_buffers=128MB;expect:成功'
        self.log.info(step1)
        gs_guc_set_cmd = "shared_buffers = 128MB"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.log.info(gs_guc_set_result)
        self.assertTrue(gs_guc_set_result, "执行失败" + step1)
        self.primary_sh.restart_db_cluster()
        result = self.com.show_param("shared_buffers")
        self.assertIn('128MB', result, "执行失败" + step1)

        step2 = '--step2:stop数据库集群;expect:成功'
        self.log.info(step2)
        self.primary_sh.stop_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step2)

        step3 = '--step3:系统配置大页数量为0，数据库用户属组无使用大页权限配置;expect:成功'
        self.log.info(step3)
        cat_hugepagesize_cmd = "grep -i Hugepagesize /proc/meminfo"
        change_hugepages_result = self.db_root_node.sh(cat_hugepagesize_cmd).result()
        self.assertIn('2048 kB', change_hugepages_result, "执行失败" + step3)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step3)
        cat_hugepages_cmd = "cat /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        cat_hugepages_result = self.db_root_node.sh(cat_hugepages_cmd).result()
        self.assertIn('0', cat_hugepages_result, "执行失败" + step3)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=1000"
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step3)
        self.assertIn('vm.hugetlb_shm_group = 1000', change_sysctl_result, "执行失败" + step3)

        step4 = '--step4:打开大页开关，设置enable_huge_pages参数为1，重启数据库;expect:打开大页开关，设置enable_huge_pages参数为1，重启数据库失败，合理报错'
        self.log.info(step4)
        gs_guc_set_cmd = "enable_huge_pages=1"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd,
                                                          single=True)
        self.assertTrue(gs_guc_set_result, "执行失败" + step4)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step4)

        step5 = '--step5:系统配置大页数量大于0，数据库用户属组无使用大页权限配置;expect:成功'
        self.log.info(step5)
        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.log.info(change_hugepages_result)
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step5)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=1000"
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.log.info(change_hugepages_result)
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step5)

        step6 = '--step6:重启数据库;expect:重启数据库失败，合理报错'
        self.log.info(step6)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step6)

        step7 = '--step7:系统配置大页数量等于0，数据库用户属组有使用大页权限配置;expect:成功'
        self.log.info(step7)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('ERROR', change_hugepages_result, "执行失败" + step7)
        select_groupid_cmd = "id -g"
        groupid = self.db_user_node.sh(select_groupid_cmd).result()
        self.log.info("groupid: " + groupid)
        self.assertNotIn('error', groupid, "执行失败" + step7)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=" + groupid
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step7)

        step8 = '--step8:重启数据库;expect:重启数据库失败，合理报错'
        self.log.info(step8)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertFalse("Normal" in status, "执行失败" + step8)

        step9 = '--step9:系统配置大页数量大于步骤8提示的required pages，数据库用户属组有使用大页权限配置;expect:成功'
        self.log.info(step9)
        change_hugepages_cmd = "echo 1000 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step9)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=" + groupid
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step9)

        step10 = '--step10:重启数据库;expect:成功'
        self.log.info(step10)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step10)

    def tearDown(self):
        step11 = '--step11:清理环境;expect:成功'
        self.log.info(step11)
        gs_guc_set_cmd = f"shared_buffers=" + self.shared_buffers_original_result
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.assertTrue(gs_guc_set_result, "执行失败" + step11)
        gs_guc_set_cmd = f"enable_huge_pages = off"
        gs_guc_set_result = self.primary_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG, gs_guc_set_cmd)
        self.assertTrue(gs_guc_set_result, "执行失败" + step11)
        self.primary_sh.restart_db_cluster()
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Normal" in status, "执行失败" + step11)
        enable_huge_pages = self.com.show_param("enable_huge_pages")
        self.log.info(enable_huge_pages)
        self.assertEqual('off', enable_huge_pages, "执行失败" + step11)
        change_hugepages_cmd = "echo 0 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
        change_hugepages_result = self.db_root_node.sh(change_hugepages_cmd).result()
        self.assertNotIn('error', change_hugepages_result, "执行失败" + step11)
        change_sysctl_cmd = "sysctl -w vm.hugetlb_shm_group=0"
        change_sysctl_result = self.db_root_node.sh(change_sysctl_cmd).result()
        self.assertNotIn('error', change_sysctl_result, "执行失败" + step11)
        self.log.info(change_sysctl_result)
        self.assertIn('vm.hugetlb_shm_group', change_sysctl_result, "执行失败" + step11)
        use_sysctl_cmd = "sysctl -p"
        self.db_root_node.sh(use_sysctl_cmd)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
